package com.tengroup.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tengroup.entity.StudentScoreEntity;
import com.tengroup.service.StudentScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/studentScore")
public class StudentScoreController {
    @Autowired
    private StudentScoreService studentScoreService;
    @GetMapping("/queryPaperId")
    public ResponseEntity<Object> QueryPaperId(
            @RequestParam(required = false) Long paperId,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize){
        try {
            StudentScoreEntity studentScoreEntity = new StudentScoreEntity();
            studentScoreEntity.setPaperId(paperId);
            IPage<StudentScoreEntity> result=studentScoreService.QueryPaperId(studentScoreEntity,pageNo,pageSize);
            return ResponseEntity.ok(result);

        } catch (Exception e) {
            return ResponseEntity.status(500).body("Error during query: " + e.getMessage());
        }
    }
    @GetMapping("/queryClassId")
    public ResponseEntity<Object> QueryClassId(
            @RequestParam(required = false) Long classId,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize){
        try {
            StudentScoreEntity studentScoreEntity = new StudentScoreEntity();
            studentScoreEntity.setClassId(classId);
            IPage<StudentScoreEntity> result=studentScoreService.QueryClassId(studentScoreEntity,pageNo,pageSize);
            return ResponseEntity.ok(result);

        } catch (Exception e) {
            return ResponseEntity.status(500).body("Error during query: " + e.getMessage());
        }
    }
    @GetMapping("/queryUserId")
    public ResponseEntity<Object> QueryUserId(
            @RequestParam(required = false) Long userId,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize){
        try {
            StudentScoreEntity studentScoreEntity = new StudentScoreEntity();
            studentScoreEntity.setUserId(userId);
            IPage<StudentScoreEntity> result=studentScoreService.QueryUserId(studentScoreEntity,pageNo,pageSize);
            return ResponseEntity.ok(result);

        } catch (Exception e) {
            return ResponseEntity.status(500).body("Error during query: " + e.getMessage());
        }
    }
    @GetMapping("/addScore")
    public String addScore(@RequestParam int i) {
        try{
            Integer result = studentScoreService.addScore(i);
            return result == 1 ? "success" : "fail";
        }
        catch(Exception e){
            log.error("Error generating StudentScore:", e);
            return "fail";
        }
    }//半手动添加版……
    @PostMapping("/addScoreByHand")
    public String addScoreByHand(@RequestBody StudentScoreEntity studentScoreEntity) {
        try{
            Integer result = studentScoreService.addScoreByHand(studentScoreEntity);
            return result == 1 ? "success" : "fail";
        }
        catch(Exception e){
            log.error("Error generating StudentScore:", e);
            return "fail";
        }
    }//全手动添加版
    @PostMapping("/updateScore")
    public String updateScore(@RequestBody StudentScoreEntity studentScoreEntity) {
        try {
            Integer result = studentScoreService.updateScore(studentScoreEntity);
            return result == 1 ? "success" : "fail";
        } catch (Exception e) {
            log.error("Error updating StudentScore:", e);
            return "fail";
        }
    }

    @DeleteMapping("/deleteScore/{id}")
    public String deleteScore(@PathVariable Long id) {
        try {
            Integer result = studentScoreService.deleteScore(id);
            return result == 1 ? "success" : "fail";
        } catch (Exception e) {
            log.error("Error deleting StudentScore:", e);
            return "fail";
        }
    }
    @GetMapping("/getScoreByUserId")
    public List<StudentScoreEntity> getScoreByUserId(@RequestParam Long userId){
        return studentScoreService.getScoreByUserId(userId);
    }

    @GetMapping("/getScoreByClassId")
    public List<StudentScoreEntity> getScoreByClassId(@RequestParam Long classId){
        return studentScoreService.getScoreByClassId(classId);
    }
    @GetMapping("/getUserAverage")
    public ResponseEntity<Object> getAverageScoreByPaperIdAndUserId(
            @RequestParam Long paperId,
            @RequestParam Long userId) {
        try {
            Double averageScore = studentScoreService.getAverageScoreByPaperIdAndUserId(paperId, userId);
            if (averageScore != null) {
                return ResponseEntity.ok(averageScore);
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No score found for the given paperId and userId");
            }
        } catch (Exception e) {
            return ResponseEntity.status(500).body("Error during query: " + e.getMessage());
        }
    }
    @GetMapping("/getClassAverage")
    public ResponseEntity<Object> getAverageScoreByPaperIdAndClassIdAndExamTime(
            @RequestParam Long paperId,
            @RequestParam Long classId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime examTime) {
        try {
            Double averageScore = studentScoreService.getAverageScoreByPaperIdAndClassIdAndExamTime(paperId, classId, examTime);
            if (averageScore != null) {
                return ResponseEntity.ok(averageScore);
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No score found for the given paperId and classId and examTime");
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error during query: " + e.getMessage());
        }
    }
}
